<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Vue要做权限管理该怎么做？如果控制到按钮级别的权限怎么做？ | 冲冲冲</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/wxb-blog/touXiang.jpg">
    <meta name="description" content="前端知识点整理">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/wxb-blog/assets/css/0.styles.c4047da6.css" as="style"><link rel="preload" href="/wxb-blog/assets/js/app.8c0fb757.js" as="script"><link rel="preload" href="/wxb-blog/assets/js/4.900b5326.js" as="script"><link rel="preload" href="/wxb-blog/assets/js/1.627dfec7.js" as="script"><link rel="preload" href="/wxb-blog/assets/js/76.ae253459.js" as="script"><link rel="prefetch" href="/wxb-blog/assets/js/10.5da366e1.js"><link rel="prefetch" href="/wxb-blog/assets/js/100.6a0e9515.js"><link rel="prefetch" href="/wxb-blog/assets/js/101.0a4f1ddb.js"><link rel="prefetch" href="/wxb-blog/assets/js/102.f8294266.js"><link rel="prefetch" href="/wxb-blog/assets/js/103.7487022c.js"><link rel="prefetch" href="/wxb-blog/assets/js/104.11246d00.js"><link rel="prefetch" href="/wxb-blog/assets/js/105.f716509d.js"><link rel="prefetch" href="/wxb-blog/assets/js/106.58efb93a.js"><link rel="prefetch" href="/wxb-blog/assets/js/107.62d831d4.js"><link rel="prefetch" href="/wxb-blog/assets/js/108.583b5a2d.js"><link rel="prefetch" href="/wxb-blog/assets/js/109.1209ce30.js"><link rel="prefetch" href="/wxb-blog/assets/js/11.31bc45c0.js"><link rel="prefetch" href="/wxb-blog/assets/js/110.2269d67b.js"><link rel="prefetch" href="/wxb-blog/assets/js/111.c9798d51.js"><link rel="prefetch" href="/wxb-blog/assets/js/112.50a5b1d8.js"><link rel="prefetch" href="/wxb-blog/assets/js/113.a138b788.js"><link rel="prefetch" href="/wxb-blog/assets/js/114.30231cae.js"><link rel="prefetch" href="/wxb-blog/assets/js/115.01c0d6b9.js"><link rel="prefetch" href="/wxb-blog/assets/js/116.3b80b466.js"><link rel="prefetch" href="/wxb-blog/assets/js/117.48830aa8.js"><link rel="prefetch" href="/wxb-blog/assets/js/118.b53a6516.js"><link rel="prefetch" href="/wxb-blog/assets/js/119.271b5e67.js"><link rel="prefetch" href="/wxb-blog/assets/js/12.a84c083f.js"><link rel="prefetch" href="/wxb-blog/assets/js/120.829532de.js"><link rel="prefetch" href="/wxb-blog/assets/js/121.5e6b5cc1.js"><link rel="prefetch" href="/wxb-blog/assets/js/122.ec3253a7.js"><link rel="prefetch" href="/wxb-blog/assets/js/123.d97dcb4a.js"><link rel="prefetch" href="/wxb-blog/assets/js/124.0a2ebde2.js"><link rel="prefetch" href="/wxb-blog/assets/js/125.2325b4c3.js"><link rel="prefetch" href="/wxb-blog/assets/js/13.b0f2669d.js"><link rel="prefetch" href="/wxb-blog/assets/js/14.ec9b0c28.js"><link rel="prefetch" href="/wxb-blog/assets/js/15.73531217.js"><link rel="prefetch" href="/wxb-blog/assets/js/16.caa982f1.js"><link rel="prefetch" href="/wxb-blog/assets/js/17.a3080f70.js"><link rel="prefetch" href="/wxb-blog/assets/js/18.3ed83fbe.js"><link rel="prefetch" href="/wxb-blog/assets/js/19.c6d34143.js"><link rel="prefetch" href="/wxb-blog/assets/js/20.1dbc75a7.js"><link rel="prefetch" href="/wxb-blog/assets/js/21.0b017963.js"><link rel="prefetch" href="/wxb-blog/assets/js/22.1add63ec.js"><link rel="prefetch" href="/wxb-blog/assets/js/23.29eb7ce7.js"><link rel="prefetch" href="/wxb-blog/assets/js/24.1acc14c0.js"><link rel="prefetch" href="/wxb-blog/assets/js/25.4336e0d3.js"><link rel="prefetch" href="/wxb-blog/assets/js/26.0f25ef0b.js"><link rel="prefetch" href="/wxb-blog/assets/js/27.610e84a6.js"><link rel="prefetch" href="/wxb-blog/assets/js/28.46cdd718.js"><link rel="prefetch" href="/wxb-blog/assets/js/29.979ca3bb.js"><link rel="prefetch" href="/wxb-blog/assets/js/30.ea83a5c0.js"><link rel="prefetch" href="/wxb-blog/assets/js/31.e3e3d825.js"><link rel="prefetch" href="/wxb-blog/assets/js/32.aa79659d.js"><link rel="prefetch" href="/wxb-blog/assets/js/33.519bc533.js"><link rel="prefetch" href="/wxb-blog/assets/js/34.532535b1.js"><link rel="prefetch" href="/wxb-blog/assets/js/35.d9c1cc94.js"><link rel="prefetch" href="/wxb-blog/assets/js/36.8d328510.js"><link rel="prefetch" href="/wxb-blog/assets/js/37.c5ee763c.js"><link rel="prefetch" href="/wxb-blog/assets/js/38.33cc76ee.js"><link rel="prefetch" href="/wxb-blog/assets/js/39.7d0f943e.js"><link rel="prefetch" href="/wxb-blog/assets/js/40.67486ae5.js"><link rel="prefetch" href="/wxb-blog/assets/js/41.5c5ebfd0.js"><link rel="prefetch" href="/wxb-blog/assets/js/42.f5596c7b.js"><link rel="prefetch" href="/wxb-blog/assets/js/43.32e8db11.js"><link rel="prefetch" href="/wxb-blog/assets/js/44.3bd8001e.js"><link rel="prefetch" href="/wxb-blog/assets/js/45.65f5b1ae.js"><link rel="prefetch" href="/wxb-blog/assets/js/46.45fc8464.js"><link rel="prefetch" href="/wxb-blog/assets/js/47.459c158e.js"><link rel="prefetch" href="/wxb-blog/assets/js/48.a11bfe4f.js"><link rel="prefetch" href="/wxb-blog/assets/js/49.9c30d506.js"><link rel="prefetch" href="/wxb-blog/assets/js/5.fb67145f.js"><link rel="prefetch" href="/wxb-blog/assets/js/50.4e4774a6.js"><link rel="prefetch" href="/wxb-blog/assets/js/51.9a1b5ee4.js"><link rel="prefetch" href="/wxb-blog/assets/js/52.63ea264a.js"><link rel="prefetch" href="/wxb-blog/assets/js/53.84a598c3.js"><link rel="prefetch" href="/wxb-blog/assets/js/54.a4b338f0.js"><link rel="prefetch" href="/wxb-blog/assets/js/55.e8702b05.js"><link rel="prefetch" href="/wxb-blog/assets/js/56.9f6390ba.js"><link rel="prefetch" href="/wxb-blog/assets/js/57.59f004d1.js"><link rel="prefetch" href="/wxb-blog/assets/js/58.41470fa8.js"><link rel="prefetch" href="/wxb-blog/assets/js/59.b48e3490.js"><link rel="prefetch" href="/wxb-blog/assets/js/6.4c0abb46.js"><link rel="prefetch" href="/wxb-blog/assets/js/60.4c84f1b7.js"><link rel="prefetch" href="/wxb-blog/assets/js/61.ae0fe02f.js"><link rel="prefetch" href="/wxb-blog/assets/js/62.b4e4a1e6.js"><link rel="prefetch" href="/wxb-blog/assets/js/63.ec0dee52.js"><link rel="prefetch" href="/wxb-blog/assets/js/64.3d6d64aa.js"><link rel="prefetch" href="/wxb-blog/assets/js/65.c669dbe2.js"><link rel="prefetch" href="/wxb-blog/assets/js/66.47a199e3.js"><link rel="prefetch" href="/wxb-blog/assets/js/67.b96f5161.js"><link rel="prefetch" href="/wxb-blog/assets/js/68.f2917e43.js"><link rel="prefetch" href="/wxb-blog/assets/js/69.1f0baea0.js"><link rel="prefetch" href="/wxb-blog/assets/js/7.e2ab66a8.js"><link rel="prefetch" href="/wxb-blog/assets/js/70.4f25a08f.js"><link rel="prefetch" href="/wxb-blog/assets/js/71.b319631d.js"><link rel="prefetch" href="/wxb-blog/assets/js/72.e0191932.js"><link rel="prefetch" href="/wxb-blog/assets/js/73.ecb5024d.js"><link rel="prefetch" href="/wxb-blog/assets/js/74.c6e1feec.js"><link rel="prefetch" href="/wxb-blog/assets/js/75.6daefc45.js"><link rel="prefetch" href="/wxb-blog/assets/js/77.aeeda56a.js"><link rel="prefetch" href="/wxb-blog/assets/js/78.acbdc69b.js"><link rel="prefetch" href="/wxb-blog/assets/js/79.06ee3013.js"><link rel="prefetch" href="/wxb-blog/assets/js/8.457c5be9.js"><link rel="prefetch" href="/wxb-blog/assets/js/80.3e6d4caf.js"><link rel="prefetch" href="/wxb-blog/assets/js/81.e34252a0.js"><link rel="prefetch" href="/wxb-blog/assets/js/82.c2c5018f.js"><link rel="prefetch" href="/wxb-blog/assets/js/83.98e1596b.js"><link rel="prefetch" href="/wxb-blog/assets/js/84.ad81eee2.js"><link rel="prefetch" href="/wxb-blog/assets/js/85.b9f50159.js"><link rel="prefetch" href="/wxb-blog/assets/js/86.02892036.js"><link rel="prefetch" href="/wxb-blog/assets/js/87.46d2a524.js"><link rel="prefetch" href="/wxb-blog/assets/js/88.c4be6ad8.js"><link rel="prefetch" href="/wxb-blog/assets/js/89.6a428824.js"><link rel="prefetch" href="/wxb-blog/assets/js/9.23625e81.js"><link rel="prefetch" href="/wxb-blog/assets/js/90.56b8afcf.js"><link rel="prefetch" href="/wxb-blog/assets/js/91.362cd602.js"><link rel="prefetch" href="/wxb-blog/assets/js/92.73449f51.js"><link rel="prefetch" href="/wxb-blog/assets/js/93.4867589e.js"><link rel="prefetch" href="/wxb-blog/assets/js/94.54b5078f.js"><link rel="prefetch" href="/wxb-blog/assets/js/95.5d023adf.js"><link rel="prefetch" href="/wxb-blog/assets/js/96.35a29d7f.js"><link rel="prefetch" href="/wxb-blog/assets/js/97.3ce9e9b3.js"><link rel="prefetch" href="/wxb-blog/assets/js/98.b9799210.js"><link rel="prefetch" href="/wxb-blog/assets/js/99.4ed8d1d2.js"><link rel="prefetch" href="/wxb-blog/assets/js/vendors~flowchart.dea86b1a.js">
    <link rel="stylesheet" href="/wxb-blog/assets/css/0.styles.c4047da6.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-7dd95ae2><div data-v-7dd95ae2><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>冲冲冲</h3> <p class="description" data-v-59e6cb88>前端知识点整理</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>wxb</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div class="hide" data-v-7dd95ae2><header class="navbar" data-v-7dd95ae2><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/wxb-blog/" class="home-link router-link-active"><img src="/wxb-blog/touXiang.jpg" alt="冲冲冲" class="logo"> <span class="site-name">冲冲冲</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/wxb-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Css/" class="nav-link"><i class="undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Docker/" class="nav-link"><i class="undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/HTML/" class="nav-link"><i class="undefined"></i>
  HTML
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/React/" class="nav-link"><i class="undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/uniapp项目/" class="nav-link"><i class="undefined"></i>
  uniapp项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Vue/" class="nav-link"><i class="undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/项目/" class="nav-link"><i class="undefined"></i>
  项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/插件库/" class="nav-link"><i class="undefined"></i>
  插件库
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/开源项目/" class="nav-link"><i class="undefined"></i>
  开源项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/面试题/" class="nav-link"><i class="undefined"></i>
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/面试问/" class="nav-link"><i class="undefined"></i>
  面试问
</a></li></ul></div></div><div class="nav-item"><a href="/wxb-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      王xb
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/wxb666" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-7dd95ae2></div> <aside class="sidebar" data-v-7dd95ae2><div class="personal-info-wrapper" data-v-1fad0c41 data-v-7dd95ae2><img src="/wxb-blog/touXiang.jpg" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    wxb
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>112</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>22</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/wxb-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Css/" class="nav-link"><i class="undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Docker/" class="nav-link"><i class="undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/HTML/" class="nav-link"><i class="undefined"></i>
  HTML
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/React/" class="nav-link"><i class="undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/uniapp项目/" class="nav-link"><i class="undefined"></i>
  uniapp项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Vue/" class="nav-link"><i class="undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/项目/" class="nav-link"><i class="undefined"></i>
  项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/插件库/" class="nav-link"><i class="undefined"></i>
  插件库
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/开源项目/" class="nav-link"><i class="undefined"></i>
  开源项目
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/面试题/" class="nav-link"><i class="undefined"></i>
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/wxb-blog/categories/面试问/" class="nav-link"><i class="undefined"></i>
  面试问
</a></li></ul></div></div><div class="nav-item"><a href="/wxb-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      王xb
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/wxb666" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88></h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>wxb</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div data-v-7dd95ae2><div data-v-7dd95ae2><main class="page"><section style="display:;"><div class="page-title"><h1 class="title">Vue要做权限管理该怎么做？如果控制到按钮级别的权限怎么做？</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>王xb</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>2/23/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>项目</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="vue要做权限管理该怎么做-如果控制到按钮级别的权限怎么做"><a href="#vue要做权限管理该怎么做-如果控制到按钮级别的权限怎么做" class="header-anchor">#</a> Vue要做权限管理该怎么做？如果控制到按钮级别的权限怎么做？</h1> <h2 id="一、是什么"><a href="#一、是什么" class="header-anchor">#</a> 一、是什么</h2> <p>权限是对特定资源的访问许可，所谓权限控制，也就是确保用户只能访问到被分配的资源</p> <p>而前端权限归根结底是请求的发起权，请求的发起可能有下面两种形式触发</p> <ul><li>页面加载触发</li> <li>页面上的按钮点击触发</li></ul> <p>总的来说，所有的请求发起都触发自前端路由或视图</p> <p>所以我们可以从这两方面入手，对触发权限的源头进行控制，最终要实现的目标是：</p> <ul><li><p>路由方面，用户登录后只能看到自己有权访问的导航菜单，也只能访问自己有权访问的路由地址，否则将跳转 <code>4xx</code> 提示页</p></li> <li><p>视图方面，用户只能看到自己有权浏览的内容和有权操作的控件</p></li> <li><p>最后再加上请求控制作为最后一道防线，路由可能配置失误，按钮可能忘了加权限，这种时候请求控制可以用来兜底，越权请求将在前端被拦截</p></li></ul> <h2 id="二、如何做"><a href="#二、如何做" class="header-anchor">#</a> 二、如何做</h2> <p>前端权限控制可以分为四个方面：</p> <ul><li>接口权限</li> <li>按钮权限</li> <li>菜单权限</li> <li>路由权限</li></ul> <h3 id="接口权限"><a href="#接口权限" class="header-anchor">#</a> 接口权限</h3> <p>接口权限目前一般采用<code>jwt</code>的形式来验证，没有通过的话一般返回<code>401</code>，跳转到登录页面重新进行登录</p> <p>登录完拿到<code>token</code>，将<code>token</code>存起来，通过<code>axios</code>请求拦截器进行拦截，每次请求的时候头部携带<code>token</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>axios<span class="token punctuation">.</span>interceptors<span class="token punctuation">.</span>request<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token parameter">config</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    config<span class="token punctuation">.</span>headers<span class="token punctuation">[</span><span class="token string">'token'</span><span class="token punctuation">]</span> <span class="token operator">=</span> cookie<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'token'</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> config
<span class="token punctuation">}</span><span class="token punctuation">)</span>
axios<span class="token punctuation">.</span>interceptors<span class="token punctuation">.</span>response<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>response<span class="token punctuation">}</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>code <span class="token operator">===</span> <span class="token number">40099</span> <span class="token operator">||</span> response<span class="token punctuation">.</span>data<span class="token punctuation">.</span>code <span class="token operator">===</span> <span class="token number">40098</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//token过期或者错误</span>
        router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="路由权限控制"><a href="#路由权限控制" class="header-anchor">#</a> 路由权限控制</h3> <p><strong>方案一</strong></p> <p>初始化即挂载全部路由，并且在路由上标记相应的权限信息，每次路由跳转前做校验</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> routerMap <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span>
    <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/permission'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">component</span><span class="token operator">:</span> Layout<span class="token punctuation">,</span>
    <span class="token literal-property property">redirect</span><span class="token operator">:</span> <span class="token string">'/permission/index'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">alwaysShow</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// will always show the root menu</span>
    <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'permission'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">icon</span><span class="token operator">:</span> <span class="token string">'lock'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">roles</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'editor'</span><span class="token punctuation">]</span> <span class="token comment">// you can set roles in root nav</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token literal-property property">children</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
      <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'page'</span><span class="token punctuation">,</span>
      <span class="token function-variable function">component</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">'@/views/permission/page'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'pagePermission'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'pagePermission'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">roles</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">]</span> <span class="token comment">// or you can only set roles in sub nav</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'directive'</span><span class="token punctuation">,</span>
      <span class="token function-variable function">component</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">'@/views/permission/directive'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'directivePermission'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">title</span><span class="token operator">:</span> <span class="token string">'directivePermission'</span>
        <span class="token comment">// if do not set roles, means: this page does not require permission</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>这种方式存在以下四种缺点：</p> <ul><li><p>加载所有的路由，如果路由很多，而用户并不是所有的路由都有权限访问，对性能会有影响。</p></li> <li><p>全局路由守卫里，每次路由跳转都要做权限判断。</p></li> <li><p>菜单信息写死在前端，要改个显示文字或权限信息，需要重新编译</p></li> <li><p>菜单跟路由耦合在一起，定义路由的时候还有添加菜单显示标题，图标之类的信息，而且路由不一定作为菜单显示，还要多加字段进行标识</p></li></ul> <p><strong>方案二</strong></p> <p>初始化的时候先挂载不需要权限控制的路由，比如登录页，404等错误页。如果用户通过URL进行强制访问，则会直接进入404，相当于从源头上做了控制</p> <p>登录后，获取用户的权限信息，然后筛选有权限访问的路由，在全局路由守卫里进行调用<code>addRoutes</code>添加路由</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> router <span class="token keyword">from</span> <span class="token string">'./router'</span>
<span class="token keyword">import</span> store <span class="token keyword">from</span> <span class="token string">'./store'</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Message <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'element-ui'</span>
<span class="token keyword">import</span> NProgress <span class="token keyword">from</span> <span class="token string">'nprogress'</span> <span class="token comment">// progress bar</span>
<span class="token keyword">import</span> <span class="token string">'nprogress/nprogress.css'</span><span class="token comment">// progress bar style</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> getToken <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@/utils/auth'</span> <span class="token comment">// getToken from cookie</span>

NProgress<span class="token punctuation">.</span><span class="token function">configure</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">showSpinner</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment">// NProgress Configuration</span>

<span class="token comment">// permission judge function</span>
<span class="token keyword">function</span> <span class="token function">hasPermission</span><span class="token punctuation">(</span><span class="token parameter">roles<span class="token punctuation">,</span> permissionRoles</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>roles<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'admin'</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span> <span class="token comment">// admin permission passed directly</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>permissionRoles<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span>
  <span class="token keyword">return</span> roles<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token parameter">role</span> <span class="token operator">=&gt;</span> permissionRoles<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>role<span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> whiteList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'/login'</span><span class="token punctuation">,</span> <span class="token string">'/authredirect'</span><span class="token punctuation">]</span><span class="token comment">// no redirect whitelist</span>

router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  NProgress<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// start progress bar</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// determine if there has token</span>
    <span class="token comment">/* has token*/</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>to<span class="token punctuation">.</span>path <span class="token operator">===</span> <span class="token string">'/login'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
      NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// if current page is dashboard will not trigger	afterEach hook, so manually handle it</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>roles<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 判断当前用户是否已拉取完user_info信息</span>
        store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'GetUserInfo'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token parameter">res</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 拉取user_info</span>
          <span class="token keyword">const</span> roles <span class="token operator">=</span> res<span class="token punctuation">.</span>data<span class="token punctuation">.</span>roles <span class="token comment">// note: roles must be a array! such as: ['editor','develop']</span>
          store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'GenerateRoutes'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> roles <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span> <span class="token comment">// 根据roles权限生成可访问的路由表</span>
            router<span class="token punctuation">.</span><span class="token function">addRoutes</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>addRouters<span class="token punctuation">)</span> <span class="token comment">// 动态添加可访问路由表</span>
            <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token operator">...</span>to<span class="token punctuation">,</span> <span class="token literal-property property">replace</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record</span>
          <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
          store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'FedLogOut'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            Message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span>err <span class="token operator">||</span> <span class="token string">'Verification failed, please login again'</span><span class="token punctuation">)</span>
            <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
          <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">hasPermission</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>roles<span class="token punctuation">,</span> to<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>roles<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">//</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/401'</span><span class="token punctuation">,</span> <span class="token literal-property property">replace</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token literal-property property">query</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">noGoBack</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 可删 ↑</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">/* has no token*/</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>whiteList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>path<span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 在免登录白名单，直接进入</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span> <span class="token comment">// 否则全部重定向到登录页</span>
      NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// if current page is login will not trigger afterEach hook, so manually handle it</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

router<span class="token punctuation">.</span><span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  NProgress<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// finish progress bar</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><p>按需挂载，路由就需要知道用户的路由权限，也就是在用户登录进来的时候就要知道当前用户拥有哪些路由权限</p> <p>这种方式也存在了以下的缺点：</p> <ul><li>全局路由守卫里，每次路由跳转都要做判断</li> <li>菜单信息写死在前端，要改个显示文字或权限信息，需要重新编译</li> <li>菜单跟路由耦合在一起，定义路由的时候还有添加菜单显示标题，图标之类的信息，而且路由不一定作为菜单显示，还要多加字段进行标识</li></ul> <h3 id="菜单权限"><a href="#菜单权限" class="header-anchor">#</a> 菜单权限</h3> <p>菜单权限可以理解成将页面与理由进行解耦</p> <h4 id="方案一"><a href="#方案一" class="header-anchor">#</a> 方案一</h4> <p>菜单与路由分离，菜单由后端返回</p> <p>前端定义路由信息</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
    <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;login&quot;</span><span class="token punctuation">,</span>
    <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">&quot;/login&quot;</span><span class="token punctuation">,</span>
    <span class="token function-variable function">component</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;@/pages/Login.vue&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>name</code>字段都不为空，需要根据此字段与后端返回菜单做关联，后端返回的菜单信息中必须要有<code>name</code>对应的字段，并且做唯一性校验</p> <p>全局路由守卫里做判断</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">hasPermission</span><span class="token punctuation">(</span><span class="token parameter">router<span class="token punctuation">,</span> accessMenu</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>whiteList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>router<span class="token punctuation">.</span>path<span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">let</span> menu <span class="token operator">=</span> Util<span class="token punctuation">.</span><span class="token function">getMenuByName</span><span class="token punctuation">(</span>router<span class="token punctuation">.</span>name<span class="token punctuation">,</span> accessMenu<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>menu<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

Router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> userInfo <span class="token operator">=</span> store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>user<span class="token punctuation">.</span>userInfo<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>userInfo<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">&quot;GetUserInfo&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">await</span> store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'updateAccessMenu'</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>to<span class="token punctuation">.</span>path <span class="token operator">===</span> <span class="token string">'/login'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'home_index'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token comment">//Util.toDefaultPage([...routers], to.name, router, next);</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token operator">...</span>to<span class="token punctuation">,</span> <span class="token literal-property property">replace</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment">//菜单权限更新完成,重新进一次当前路由</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>whiteList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>path<span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 在免登录白名单，直接进入</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>to<span class="token punctuation">.</span>path <span class="token operator">===</span> <span class="token string">'/login'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'home_index'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">hasPermission</span><span class="token punctuation">(</span>to<span class="token punctuation">,</span> store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>accessMenu<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          Util<span class="token punctuation">.</span><span class="token function">toDefaultPage</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>accessMenu<span class="token punctuation">,</span>to<span class="token punctuation">,</span> routes<span class="token punctuation">,</span> next<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
          <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/403'</span><span class="token punctuation">,</span><span class="token literal-property property">replace</span><span class="token operator">:</span><span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>whiteList<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>path<span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 在免登录白名单，直接进入</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">let</span> menu <span class="token operator">=</span> Util<span class="token punctuation">.</span><span class="token function">getMenuByName</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>name<span class="token punctuation">,</span> store<span class="token punctuation">.</span>getters<span class="token punctuation">.</span>accessMenu<span class="token punctuation">)</span><span class="token punctuation">;</span>
  Util<span class="token punctuation">.</span><span class="token function">title</span><span class="token punctuation">(</span>menu<span class="token punctuation">.</span>title<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

Router<span class="token punctuation">.</span><span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  window<span class="token punctuation">.</span><span class="token function">scrollTo</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br></div></div><p>每次路由跳转的时候都要判断权限，这里的判断也很简单，因为菜单的<code>name</code>与路由的<code>name</code>是一一对应的，而后端返回的菜单就已经是经过权限过滤的</p> <p>如果根据路由<code>name</code>找不到对应的菜单，就表示用户有没权限访问</p> <p>如果路由很多，可以在应用初始化的时候，只挂载不需要权限控制的路由。取得后端返回的菜单后，根据菜单与路由的对应关系，筛选出可访问的路由，通过<code>addRoutes</code>动态挂载</p> <p>这种方式的缺点：</p> <ul><li>菜单需要与路由做一一对应，前端添加了新功能，需要通过菜单管理功能添加新的菜单，如果菜单配置的不对会导致应用不能正常使用</li> <li>全局路由守卫里，每次路由跳转都要做判断</li></ul> <h4 id="方案二"><a href="#方案二" class="header-anchor">#</a> 方案二</h4> <p>菜单和路由都由后端返回</p> <p>前端统一定义路由组件</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">Home</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;../pages/Home.vue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">UserInfo</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">&quot;../pages/UserInfo.vue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">home</span><span class="token operator">:</span> Home<span class="token punctuation">,</span>
    <span class="token literal-property property">userInfo</span><span class="token operator">:</span> UserInfo
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>后端路由组件返回以下格式</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;home&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">component</span><span class="token operator">:</span> <span class="token string">&quot;home&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;home&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">&quot;/userinfo&quot;</span><span class="token punctuation">,</span>
        <span class="token literal-property property">component</span><span class="token operator">:</span> <span class="token string">&quot;userInfo&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>在将后端返回路由通过<code>addRoutes</code>动态挂载之间，需要将数据处理一下，将<code>component</code>字段换为真正的组件</p> <p>如果有嵌套路由，后端功能设计的时候，要注意添加相应的字段，前端拿到数据也要做相应的处理</p> <p>这种方法也会存在缺点：</p> <ul><li>全局路由守卫里，每次路由跳转都要做判断</li> <li>前后端的配合要求更高</li></ul> <h3 id="按钮权限"><a href="#按钮权限" class="header-anchor">#</a> 按钮权限</h3> <h4 id="方案一-2"><a href="#方案一-2" class="header-anchor">#</a> 方案一</h4> <p>按钮权限也可以用<code>v-if</code>判断</p> <p>但是如果页面过多，每个页面页面都要获取用户权限<code>role</code>和路由表里的<code>meta.btnPermissions</code>，然后再做判断</p> <p>这种方式就不展开举例了</p> <h4 id="方案二-2"><a href="#方案二-2" class="header-anchor">#</a> 方案二</h4> <p>通过自定义指令进行按钮权限的判断</p> <p>首先配置路由</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
    <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/permission'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">component</span><span class="token operator">:</span> Layout<span class="token punctuation">,</span>
    <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'权限测试'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">btnPermissions</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'supper'</span><span class="token punctuation">,</span> <span class="token string">'normal'</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token comment">//页面需要的权限</span>
    <span class="token literal-property property">children</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
        <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'supper'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">component</span><span class="token operator">:</span> <span class="token function">_import</span><span class="token punctuation">(</span><span class="token string">'system/supper'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'权限测试页'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token literal-property property">btnPermissions</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">,</span> <span class="token string">'supper'</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span> <span class="token comment">//页面需要的权限</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'normal'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">component</span><span class="token operator">:</span> <span class="token function">_import</span><span class="token punctuation">(</span><span class="token string">'system/normal'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'权限测试页'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">meta</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token literal-property property">btnPermissions</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span> <span class="token comment">//页面需要的权限</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>自定义权限鉴定指令</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token comment">/**权限指令**/</span>
<span class="token keyword">const</span> has <span class="token operator">=</span> Vue<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string">'has'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token function-variable function">bind</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">el<span class="token punctuation">,</span> binding<span class="token punctuation">,</span> vnode</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 获取页面按钮权限</span>
        <span class="token keyword">let</span> btnPermissionsArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>binding<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">// 如果指令传值，获取指令参数，根据指令参数和当前登录人按钮权限做比较。</span>
            btnPermissionsArr <span class="token operator">=</span> Array<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>binding<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
            <span class="token comment">// 否则获取路由中的参数，根据路由的btnPermissionsArr和当前登录人按钮权限做比较。</span>
            btnPermissionsArr <span class="token operator">=</span> vnode<span class="token punctuation">.</span>context<span class="token punctuation">.</span>$route<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>btnPermissions<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">$_has</span><span class="token punctuation">(</span>btnPermissionsArr<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            el<span class="token punctuation">.</span>parentNode<span class="token punctuation">.</span><span class="token function">removeChild</span><span class="token punctuation">(</span>el<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 权限检查方法</span>
<span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">$_has</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> isExist <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取用户按钮权限</span>
    <span class="token keyword">let</span> btnPermissionsStr <span class="token operator">=</span> sessionStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">&quot;btnPermissions&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>btnPermissionsStr <span class="token operator">==</span> <span class="token keyword">undefined</span> <span class="token operator">||</span> btnPermissionsStr <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>value<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>btnPermissionsStr<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        isExist <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> isExist<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token punctuation">{</span>has<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><p>在使用的按钮中只需要引用<code>v-has</code>指令</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>el<span class="token operator">-</span>button @click<span class="token operator">=</span><span class="token string">'editClick'</span> type<span class="token operator">=</span><span class="token string">&quot;primary&quot;</span> v<span class="token operator">-</span>has<span class="token operator">&gt;</span>编辑<span class="token operator">&lt;</span><span class="token operator">/</span>el<span class="token operator">-</span>button<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="小结"><a href="#小结" class="header-anchor">#</a> 小结</h3> <p>关于权限如何选择哪种合适的方案，可以根据自己项目的方案项目，如考虑路由与菜单是否分离</p> <p>权限需要前后端结合，前端尽可能的去控制，更多的需要后台判断</p> <h2 id="参考文献"><a href="#参考文献" class="header-anchor">#</a> 参考文献</h2> <ul><li>https://mp.weixin.qq.com/s/b-D2eH1mLwL_FkaZwjueSw</li> <li>https://segmentfault.com/a/1190000020887109</li> <li>https://juejin.cn/post/6844903648057622536#heading-6</li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">3/6/2023, 7:59:39 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:12rem;" data-v-b57cc07c data-v-7dd95ae2><li class="level-2" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#一、是什么" class="sidebar-link reco-side-一、是什么" data-v-b57cc07c>一、是什么</a></li><li class="level-2" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#二、如何做" class="sidebar-link reco-side-二、如何做" data-v-b57cc07c>二、如何做</a></li><li class="level-3" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#接口权限" class="sidebar-link reco-side-接口权限" data-v-b57cc07c>接口权限</a></li><li class="level-3" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#路由权限控制" class="sidebar-link reco-side-路由权限控制" data-v-b57cc07c>路由权限控制</a></li><li class="level-3" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#菜单权限" class="sidebar-link reco-side-菜单权限" data-v-b57cc07c>菜单权限</a></li><li class="level-3" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#按钮权限" class="sidebar-link reco-side-按钮权限" data-v-b57cc07c>按钮权限</a></li><li class="level-3" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#小结" class="sidebar-link reco-side-小结" data-v-b57cc07c>小结</a></li><li class="level-2" data-v-b57cc07c><a href="/wxb-blog/views/%E6%8A%80%E6%9C%AF/Vue/Vue2/%E9%A1%B9%E7%9B%AE/Vue%E8%A6%81%E5%81%9A%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86%E8%AF%A5%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F%E6%8E%A7%E5%88%B6%E5%88%B0%E6%8C%89%E9%92%AE%E7%BA%A7%E5%88%AB%E7%9A%84%E6%9D%83%E9%99%90%E6%80%8E%E4%B9%88%E5%81%9A%EF%BC%9F.html#参考文献" class="sidebar-link reco-side-参考文献" data-v-b57cc07c>参考文献</a></li></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/wxb-blog/assets/js/app.8c0fb757.js" defer></script><script src="/wxb-blog/assets/js/4.900b5326.js" defer></script><script src="/wxb-blog/assets/js/1.627dfec7.js" defer></script><script src="/wxb-blog/assets/js/76.ae253459.js" defer></script>
  </body>
</html>
